/*
	CREATED BY	:	PRADEEP GUPTA
	DATE		:	21 NOVEMBER 2006
	PURPOSE		:	ADD DYNAMIC HTML ELEMENTS
*/
var tid=1;
function addElement() {
		//alert(tid);
	flagNegative=0
	if(tid<1){
		t_pre = tid;
		tid=1;
		flagNegative = 1;
	}
	if(document.my_prog.lead.checked || document.my_prog.sale.checked)
	{
	}else
	{
		alert('Lead or Sale must be selected.');
		return false;
	}
	var flagAdd = 0;
	var mainContainer = document.getElementById('mainContainer'); 
	if(tid==1)
	{
		var upContainer = document.createElement('div');
		var divname = 'upper';
		upContainer.setAttribute('id', divname);
		upContainer.setAttribute('align', 'center');
		mainContainer.appendChild(upContainer);  
		upContainer.innerHTML='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>From</strong> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>To</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
	}
	
	var subContainer = document.createElement('div');
	var divname = 'div_' + tid;
	subContainer.setAttribute('id', divname);
	mainContainer.appendChild(subContainer);  
	var ni = document.getElementById(divname);  
	
	//	ADD DIV FOR THE LEAD 
	ele = 'lead';
	elem= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lead';
	var lead = document.getElementById(ele);
	if(lead!=null){
		if( lead.checked ){
		  var newdiv = document.createElement('div');
		  var divIdName = 'div'+ele+'_'+tid;
		  newdiv.setAttribute('id',divIdName);
		  newdiv.innerHTML = elem + tid + '&nbsp;&nbsp;&pound;<input type="text" maxlength="8" id="text'+ele+'_v'+tid+'" name="text'+ele+'_v'+tid+'" value="0.0" class="inputtier" onclick="'+"javascript:if(parseFloat(this.value)==0.0)this.value=''"+'">&nbsp;&nbsp;&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_'+tid+'" name="text'+ele+'_'+tid+'" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">&nbsp;-&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_m'+tid+'" name="text'+ele+'_m'+tid+'" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">';  
		  ni.appendChild(newdiv);  
		  flagAdd = 1;
		}
	}
	//	ADD DIV FOR THE SALE 
	var ele = 'sale';
	var elem='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sale';
	var sale = document.getElementById(ele);

	if(sale!=null){	
		if( sale.checked ){
		  var newdiv = document.createElement('div');
		  var divIdName = 'div'+ele+'_'+tid;
		  newdiv.setAttribute('id',divIdName);
		  newdiv.innerHTML = elem + tid + '&nbsp;&nbsp;&pound;<input type="text"  maxlength="8" name="text'+ele+'_v'+tid+'" id="text'+ele+'_v'+tid+'" value="0.0" class="inputtier" onclick="'+"javascript:if(parseFloat(this.value)==0.0)this.value=''"+'">&nbsp;&nbsp;&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_'+tid+'" name="text'+ele+'_'+tid+'" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">&nbsp;-&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_m'+tid+'" name="text'+ele+'_m'+tid+'" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">';  
		  ni.appendChild(newdiv);  
		  flagAdd = 1;
		}
	}
	
	if( flagAdd ){
	  tid= tid+1;
	 }
	 else{
		
		if(flagNegative){	//	IF VALUE OF THE COUNTER WAS LESS THEN 1 AND NO NODE HAVE BEEN ADDED
			tid = t_pre;
		}
		mainContainer.removeChild(ni);	// IF NO ELEMENT IS ADDED !! REMOVE THE ADDED CHILD NODE 
	 }

}

function removeElement() {	  
	var flagRmv = 0;
 	var ni = document.getElementById('mainContainer');  
  
	  //	ADD DIV FOR THE SALE 
		var divIdName = 'div_'+(tid-1);
		var olddiv = document.getElementById(divIdName);
		if( olddiv )
		{
			ni.removeChild(olddiv);
			flagRmv = 1;
  		}
  
	  if( flagRmv )
	  {
		tid= tid-1;  
		if(tid==1)
		{
			ni.removeChild(upper);	
		}
	
	  }
  //alert(tid)
}


function addRemoveElement(ele)
{
	if(ele=='lead')
	{
	  elem='Lead';
	}
	if(ele=='sale')
	{
	  elem='&nbsp;Sale';
	}
	
	if(document.my_prog.comm_base[0].checked==true || document.my_prog.comm_base[1].checked==true)
	{
		var checkObj = document.getElementById(ele);	
		if( checkObj.checked )
		{	
					var newdiv = document.createElement('div');
					var divIdName = 'div' + ele ;
					
					if( !document.getElementById(divIdName) )
					{
						newdiv.setAttribute('id',divIdName);
						innerhtml = elem + '&nbsp;&nbsp;&pound;<input type="text"  maxlength="8" name="text'+ele+'_v1' + '"  id="text'+ele+'_v1' + '" value="0.0" class="inputtier" onclick="'+"javascript:if(parseFloat(this.value)==0.0)this.value=''"+'">'; 
						newdiv.innerHTML = innerhtml;
						fixedContainer.appendChild(newdiv);  
					}
		}
		else
		{
					var newdiv = document.createElement('div');
					var divIdName = 'div' + ele ;
					var removediv = document.getElementById(divIdName);
				   if( removediv )
				   {					
						fixedContainer.removeChild(removediv);  
					}
		}
				
	}
	else
	{
		if(ele=='lead')
		{
		  elem= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Lead';
		}
		if(ele=='sale')
		{
		  elem='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sale';
		}
		
		var checkObj = document.getElementById(ele);	
		if( checkObj.checked )
		{	//	ADD ALL ELEMENTS FOR THIS CHCKBOX	//
							
			//	IF NO NODE IS ADDED STILL -- ADD FIRST NODE
			if( tid<=1 )
			{
				addElement()
			}
			//	-------- END OF ADDING FIRST NODE	------------------
	
			//	ADD DIV FOR THE ele
			for(i=1; i<tid && tid>0; i++)
			{					
				
				
				//GET CONTAINER DIV
				var divname = 'div_' + i;
				var mainContainer = document.getElementById(divname);
				if(mainContainer )
				{
					var newdiv = document.createElement('div');
					var divIdName = 'div' + ele + '_' + i;
					
					if( !document.getElementById(divIdName) )
					{
						newdiv.setAttribute('id',divIdName);
	
						innerhtml = elem + i + '&nbsp;&nbsp;&pound;<input type="text"  maxlength="8" name="text'+ele+'_v' + i + '"  id="text'+ele+'_v' + i + '" value="0.0" class="inputtier" onclick="'+"javascript:if(parseFloat(this.value)==0.0)this.value=''"+'">&nbsp;&nbsp;&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_' + i + '" name="text'+ele+'_' + i + '" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">&nbsp;-&nbsp;<input type="text"  maxlength="8" id="text'+ele+'_m' + i + '" name="text'+ele+'_m' + i + '" value="0" class="inputtier" onclick="'+"javascript:if(parseInt(this.value)==0)this.value=''"+'">';  
	
						newdiv.innerHTML = innerhtml;
						mainContainer.appendChild(newdiv);  
					}
				}
							
			}	//	END OF FOR LOOP	
			
		}else
		{	
		//	REMOVE ALL ADDED ELEMENTS FOR THIS CHCKBOX	//
			for(i=1; i<=tid; i++){					
				
				//GET CONTAINER DIV
				var divname = 'div_' + i;
				var mainContainer = document.getElementById(divname);
	           // alert(mainContainer);
				if( mainContainer ){
					var newdiv = document.createElement('div');
					var divIdName = 'div' + ele + '_' + i;
					var removediv = document.getElementById(divIdName);
					if( removediv ){					
						mainContainer.removeChild(removediv);  
					}
				}
							
			}	//	END OF FOR LOOP
			
			//	CHECK IF THERE IS NO ELEMENT-- INITILIAZE COUNTER TO 1.
			divname = 'div' + 'sale' + '_' + 1
			div1 = document.getElementById(divname);
			divname = 'div' + 'lead' + '_' + 1
			div2 = document.getElementById(divname);
		/*	divname = 'div' + 'click' + '_' + 1
			div3 = document.getElementById(divname);*/
			if( div1 || div2 )
			{
	
			}else{
				// REMOVE ALL SUB DIV
				var mainContainer = document.getElementById('mainContainer');  
				for(i=1; i<=tid && mainContainer; i++){					
					
					//GET CONTAINER DIV
					var divname = 'div_' + i;
					var removediv = document.getElementById(divname);
					if( removediv ){					
						mainContainer.removeChild(removediv); 
						
					}
					var upperdiv = document.getElementById('upper');
					if( upperdiv )
					{	
						mainContainer.removeChild(upperdiv);	
					}
				}
				tid=0;
			}
		}
		//alert(tid);
	}
	
}

function addClickElement(click_opt)
{
	var divContent='Click &nbsp;&pound;<input name=click_v id=click_v type=text  value="0.0" class="inputtier" onclick="'+"javascript:if(parseFloat(this.value)==0.0)this.value=''"+'">'; 
	if(click_opt==true)
	{
		document.getElementById('clickContainer').innerHTML=divContent;
	}
	else
	{
		document.getElementById('clickContainer').innerHTML='';
	}
}

function check_this(obj)
{
	//alert(document.getElementById('fixedContainer').innerHTML);
	//alert(document.getElementById('mainContainer').innerHTML);
	if(obj=='FE')
	{
		document.getElementById('fixedContainer').innerHTML='';
		document.getElementById('arele').style.visibility='visible';
		addRemoveElementALl();
	}
	if(obj=='P' || obj=='F')
	{
		addRemoveElementALl();
		document.getElementById('fixedContainer').innerHTML='';
		document.getElementById('arele').style.visibility='hidden';
	}
		
			document.getElementById('lead').checked=false;
			document.getElementById('sale').checked=false;
		
}
function addRemoveElementALl()
{
	a=1;
	while(tid>1)
	{
	  //alert(tid);
	  removeElement();
	  a++;
	  if(a>20)
	  {
		tid=1;
		break;
	  }
	}
	
}
